package com.huike.report.service.impl;

import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.domain.TbClue;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.contract.mapper.TbContractMapper;
import com.huike.report.domain.vo.IndexVo;
import com.huike.report.service.IndexVoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
public class IndexVoServiceImpl implements IndexVoService {

    @Autowired
    private TbClueMapper tbClueMapper;

    @Autowired
    private TbBusinessMapper tbBusinessMapper;

    @Autowired
    private TbContractMapper tbContractMapper;

    @Override
    public IndexVo getIndex(LocalDate beginCreateTime, LocalDate endCreateTime) {

        //获得当天的开始时间
        LocalDateTime begin = LocalDateTime.of(beginCreateTime, LocalTime.MIN);
        LocalDateTime begin1 = LocalDateTime.of(LocalDate.now(),  LocalTime.MIN);
        //获得当天的结束时间
        LocalDateTime end = LocalDateTime.of(endCreateTime, LocalTime.MAX);
        LocalDateTime end1 = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);

        //线索数量
        int cluesNum = tbClueMapper.cluesNum(begin, end,null);
        //今日新增线索数量
        int todayCluesNum = tbClueMapper.todayCluesNum(begin1,end1,null);
        //待跟进线索数量
        int tofollwedCluesNum = tbClueMapper.tofollwedCluesNum(begin, end,TbClue.StatusType.UNFOLLOWED.getValue());
        //待分配线索
        int toallocatedCluesNum = tbClueMapper.toallocatedCluesNum(begin, end);
        //商机数量
        int businessNum =   tbBusinessMapper .businessNum(begin,end,null);
        //今日新增商机数量
        int todayBusinessNum =   tbBusinessMapper .todayBusinessNum(begin1,end1,null);
        //待跟进商机数量
        int tofollwedBusinessNum =   tbBusinessMapper .tofollwedBusinessNum(begin,end,TbClue.StatusType.UNFOLLOWED.getValue());
        //待分配商机
        int toallocatedBusinessNum =   tbBusinessMapper .toallocatedBusinessNum(begin,end);
        //客户数量
        int contractNum =   tbContractMapper. contractNum(begin,end,TbClue.StatusType.FALSE.getValue());
        //今日新增客户数量
        int todayContractNum =   tbContractMapper. todayContractNum(begin1,end1,null);

        //销售金额
        Double salesAmount =   tbContractMapper. salesAmount(begin,end, TbClue.StatusType.FALSE.getValue());
        salesAmount= (salesAmount==null)?0:salesAmount;
        //今日新增销售额
        Double todaySaleAmount =   tbContractMapper. todaySaleAmount(begin1,end1, TbClue.StatusType.FALSE.getValue());
        IndexVo indexVo = new IndexVo();
        indexVo.setCluesNum(cluesNum);
        indexVo.setTodayCluesNum(todayCluesNum);
        indexVo.setTofollowedCluesNum(tofollwedCluesNum);
        indexVo.setToallocatedCluesNum(toallocatedCluesNum);

        indexVo.setBusinessNum(businessNum);
        indexVo.setTodayBusinessNum(todayBusinessNum);
        indexVo.setTofollowedBusinessNum(tofollwedBusinessNum);
        indexVo.setToallocatedBusinessNum(toallocatedBusinessNum);

        indexVo.setContractNum(contractNum);
        indexVo.setTodayContractNum(todayContractNum);

        indexVo.setSalesAmount(salesAmount);
        indexVo.setTodaySalesAmount(todaySaleAmount);
        return indexVo;
    }
}
